# -*- coding: utf-8 -*-
import sys
sys.path.append('../..')
from common.config import *
from db.mysql import MysqlClient
from urllib.parse import urlencode
import requests
from common.helper import Helper


class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "e享人生百万医疗保险"
    def genProductInfo(self):
        pass
    def requestJson(self,dt=None,mysql=None):
        data = {
            "productNo": "3008",
            "rationType": "1" if dt["socialSec"] =="Y" else "2",
            "num": "1",
            "age": dt["year"],
            "source": "G"
        }
        payload = urlencode( data ).replace("+","")
        r = requests.post(url = domain[self.name]["url"],headers=domain[self.name]["header"],data=payload)
        if r.status_code == 200 and "premium" in r.text:
            price = Helper.decimalParse( r.json()["premium"] )
            data = {
                "name":self.name,
                "id":dt["id"],
                "price":price
            }
            mysql.updatePrice( data )
        else:
            data = {
                "name":self.name,
                "id":dt["id"],
                "state":3
            }
            mysql.updateTask( data )
            print("\n/*更新产品价格数据失败：%d*/"%dt["id"])
